package com.linxiao.call.controller;

import com.linxiao.call.domain.CrmWebcallFile;
import com.linxiao.call.service.ICrmWebcallFileService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.manager.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 语音通知批次Controller
 *
 * @author qiqi
 * @date 2022-06-28
 */
@RestController
@RequestMapping("/crm/file")
public class CrmWebcallFileController extends BaseController {
    @Autowired
    private ICrmWebcallFileService crmWebcallFileService;
	@Resource
	private AdminManager adminManager;
    /**
     * 查询语音通知批次列表
     */
    @PreAuthorize("@ss.hasPermi('crm:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmWebcallFile crmWebcallFile) {
    	LoginUser loginUser = SecurityUtils.getLoginUser();
		// 当前登录人是否是管理员部门
		if (!adminManager.loginUserIsAdminDept(loginUser)) {
			crmWebcallFile.setMchId(loginUser.getUser().getMchId());
		}
        startPage();
        List<CrmWebcallFile> list = crmWebcallFileService.selectCrmWebcallFileList(crmWebcallFile);
        return getDataTable(list);
    }

    /**
     * 导出语音通知批次列表
     */
    @PreAuthorize("@ss.hasPermi('crm:file:export')")
    @Log(title = "语音通知批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmWebcallFile crmWebcallFile) {
    	LoginUser loginUser = SecurityUtils.getLoginUser();
		// 当前登录人是否是管理员部门
		if (!adminManager.loginUserIsAdminDept(loginUser)) {
			crmWebcallFile.setMchId(loginUser.getUser().getMchId());
		}
        List<CrmWebcallFile> list = crmWebcallFileService.selectCrmWebcallFileList(crmWebcallFile);
        ExcelUtil<CrmWebcallFile> util = new ExcelUtil<CrmWebcallFile>(CrmWebcallFile.class);
        util.exportExcel(response, list, "语音通知批次数据");
    }

    /**
     * 获取语音通知批次详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(crmWebcallFileService.selectCrmWebcallFileById(id));
    }

    /**
     * 新增语音通知批次
     */
    @PreAuthorize("@ss.hasPermi('crm:file:add')")
    @Log(title = "语音通知批次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmWebcallFile crmWebcallFile) {

        if (crmWebcallFile.getStartTime() != null && crmWebcallFile.getOverTime() != null) {//NOSONAR
            if (crmWebcallFile.getStartTime().getTime() > crmWebcallFile.getOverTime().getTime()) {//NOSONAR
                return AjaxResult.error(HttpStatus.ERROR, "开始时间不能大于结束时间！！！");
            }
        }

        return toAjax(crmWebcallFileService.insertCrmWebcallFile(crmWebcallFile));
    }

    /**
     * 修改语音通知批次
     */
    @PreAuthorize("@ss.hasPermi('crm:file:edit')")
    @Log(title = "语音通知批次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmWebcallFile crmWebcallFile) {
        return toAjax(crmWebcallFileService.updateCrmWebcallFile(crmWebcallFile));
    }

    /**
     * 删除语音通知批次
     */
    @PreAuthorize("@ss.hasPermi('crm:file:remove')")
    @Log(title = "语音通知批次", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return crmWebcallFileService.deleteCrmWebcallFileByIds(ids);
    }

    /**
     * 启动语音通知批次任务
     */
    @Log(title = "语音通知批次任务启动", businessType = BusinessType.LIST)
    @GetMapping("/start/{id}")
    public AjaxResult start(@PathVariable("id") Long id) {

        AjaxResult ajaxResult = crmWebcallFileService.start(id);
        return ajaxResult;
    }
    /**
     * 启动语音通知批次任务
     */
    @Log(title = "语音通知批次任务取消", businessType = BusinessType.LIST)
    @GetMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id) {

        AjaxResult ajaxResult = crmWebcallFileService.cancel(id);
        return ajaxResult;
    }

}
